package com.hl.sfzy.controller;

import com.hl.sfzy.constant.CommonConstant;
import com.hl.sfzy.constant.SystemCode;
import com.hl.sfzy.entity.Consult;
import com.hl.sfzy.service.ConsultService;
import com.hl.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName ConsultController
 * @Description WHATETODO
 * @Author Wxx
 * @Date 2024年01月15日 10:17
 * @Version 1.0
 */
@CrossOrigin
@RestController
@RequestMapping("/consult")
public class ConsultController {
    @Autowired
    ConsultService consultService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public com.hl.sfzy.utils.Result add(@RequestBody Consult consult) throws Exception {
        consultService.add(consult);
        return Result.success();
    }

    /**
     * 编辑
     */
    @PutMapping("/edit")
    public Result edit(@RequestBody Consult consult) {
        consultService.edit(consult);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Integer id) {
        consultService.remove(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/remove")
    public Result removeBatch(Integer[] ids) {
        System.out.println(ids);
        consultService.removeBatch(ids);
        return Result.success();
    }

    /**
     * 单条查询
     */
    @GetMapping("/find/{id}")
    public Result findById(@PathVariable Integer id) {
        return Result.success(consultService.findById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/find")
    public Result findPage(@RequestParam(value = CommonConstant.PAGE_NUM, required = false, defaultValue = CommonConstant.PAGE_NUM_DEFAULT) Integer pageNum,
                                             @RequestParam(value = CommonConstant.PAGE_SIZE, required = false, defaultValue = CommonConstant.PAGE_SIZE_DEFAULT) Integer pageSize,
                                             Consult consult) {
        return Result.success(consultService.findPage(pageNum, pageSize, consult));
    }

    /**
     * 判断用户名是否重复
     */
    @GetMapping("/findExists/{name}")
    public Result findUsernameExists(@PathVariable String name) {
        Consult consult = consultService.selectByName(name);
        if (consult == null) {
            return Result.success();
        }
        return Result.error(SystemCode.USERNAME_EXISTS.getCode(), SystemCode.USERNAME_EXISTS.getMessage(), null);
    }

}